//
//  TrainingDetailViewController.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/18.
//

import UIKit
import MYModule
import CRPSmartRing
import RxCocoa
import RxSwift

open class TrainingDetailViewController: TableViewController {
    
    public var showRightItem: Bool = true
    
    /// 目标是否达成
    public var achievement: Bool = false
    
    
    open var viewModel: TrainingDetailViewModel? {
        
        didSet {
            self.navigation.item.title = viewModel?.type.title
            tableView.reloadData()
            
        }
    }
    
    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        
        setNavigationBar()
        
        
    }
    
    open override func backAction() {
        
        for controller in self.navigationController!.viewControllers where controller is TrainingRecordsController {
            self.navigationController?.popViewController(animated: true)
            return
        }
        
        for controller in self.navigationController!.viewControllers where controller is ActivityHomeController {
            self.navigationController?.popViewController(animated: true)
            return
        }
        
        self.navigationController?.popToRootViewController(animated: true)
        
    }
    
    open func setNavigationBar() {
        
        if showRightItem {
            navigation.item.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "ic_data"), style: .plain, target: self, action: #selector(pushToHistory))
        }
        
        navigation.bar.tintColor = UIColor.assist_222222
        navigation.bar.backgroundColor = UIColor.background
        navigation.bar.titleTextAttributes = [NSAttributedString.Key.font: Fonts.boldSystemFont(ofSize: 17)]
        
//        navigation.bar
        
    }
    
    @objc open func pushToHistory() {
        
        let vc = TrainingRecordsController()
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    
    
    open override func makeUI() {
        super.makeUI()
        
        tableView.backgroundColor =  UIColor.darkBackground
        tableView.register(TrainingDetailworkoutsRecordCell.self, forCellReuseIdentifier: NSStringFromClass(TrainingDetailworkoutsRecordCell.self))
        tableView.register(TrainingDetailSubDataCell.self, forCellReuseIdentifier: NSStringFromClass(TrainingDetailSubDataCell.self))
        tableView.register(GPSTrainingHeartChartTableViewCell.self, forCellReuseIdentifier: NSStringFromClass(GPSTrainingHeartChartTableViewCell.self))
        tableView.register(TrainingDetailHeartRateZoneCell.self, forCellReuseIdentifier: NSStringFromClass(TrainingDetailHeartRateZoneCell.self))
        
        tableView.dataSource = self
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

    }
    
    open override func bindViewModel() {
        
        if achievement == true {
            
            BluetoothResponder.shared.trainingGoalAlert(type: .trainingGoal)
            
        }
        
    }
    
}

extension TrainingDetailViewController: UITableViewDataSource {
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.row == 0 {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(TrainingDetailworkoutsRecordCell.self), for: indexPath) as! TrainingDetailworkoutsRecordCell
            
            cell.totalData = viewModel?.runTimeRelay.value
            
            return cell
            
        }
        
        if indexPath.row == 1 {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(TrainingDetailSubDataCell.self), for: indexPath) as! TrainingDetailSubDataCell
            
            cell.totalData = viewModel?.runTimeRelay.value
            
            return cell
            
        }
        
        
        if indexPath.row == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(GPSTrainingHeartChartTableViewCell.self), for: indexPath) as! GPSTrainingHeartChartTableViewCell
           
            cell.trainingModel = viewModel?.trainingModelRelay.value
 
            return cell
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(TrainingDetailHeartRateZoneCell.self), for: indexPath) as! TrainingDetailHeartRateZoneCell
        
        if let dataAnalysisViewModel = viewModel?.heartRateZoneRelay.value {
            cell.heartRateZoneModel = dataAnalysisViewModel
            
        }

        return cell
        
    }
    
    
    
    
    
}
